package com.ganbo.rabbitmq.delay;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟队列插件配置
 *
 * @Author: ganbo
 * @Date: 2020/12/9 19:32
 */

@Configuration
public class DelayQueueConfig {

    //交换机
    public final static String EXCHANGE_DELAY = "ganbo.exchange.delay";

    //队列
    public final static String TOPIC_DELAY = "ganbo.topic.delay";

    //routing key(绑定建)
    public final static String ROUTING_KEY_DELAY = "ganbo.routingKey.delay";

    //声明延迟交换机
    @Bean
    public TopicExchange exchange_delay() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "topic");
        return new TopicExchange(EXCHANGE_DELAY, true, false, args);
    }


    //声明队列
    @Bean
    public Queue queue_delay() {
        return new Queue(TOPIC_DELAY, true);
    }


    //绑定队列
    //将queue和exchange绑定,而且绑定的键值为topic.pay
    //这样只要是消息携带的路由键是routing.pay,才会分发到该队列
    @Bean
    public Binding bindingExchangeMessage_delay() {
        return BindingBuilder.bind(queue_delay()).to(exchange_delay()).with(ROUTING_KEY_DELAY);
    }

}
